/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Record.java
 *
 * Created on 3/Dez/2011, 15:35:33
 */

package intopo.evogame.partnerselection.tools.analyser;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * Base class of all records belong to random access files.  These records have a unique primary key stored as a Java int.
 * This class provides a method that given a random access file and a record key, positions the file at the place where the record is.  This assumes the records are written to the file in ascending order.
 * @author pedro
 */
class Record
{
	/**
	 * Size in bytes of record.
	 */
	final private int recordSize;
	/**
	 * Primary key of the record.
	 */
	int ID;
	/**
	 * Sole constructor.
	 *
	 * @param recordSize size in bytes of record.
	 */
	protected Record (int recordSize)
	{
		this.recordSize = recordSize;
	}
	/**
	 *
	 * @param raf
	 * @param id
	 * @return
	 * @throws java.io.IOException
	 */
	protected boolean position (RandomAccessFile raf, int id)
		throws IOException
	{
//		System.out.println ("seeking to " + id * this.recordSize);
		raf.seek (id * this.recordSize);
		ID = raf.readInt ();
		if (ID == id) {
			return true;
		}
		long filePointer = raf.getFilePointer ();
		System.err.println (id + " is not at " + filePointer);
		while (ID < id) {
			filePointer += recordSize;
			raf.seek (filePointer);
			ID = raf.readInt ();
		}
		while (ID > id) {
			filePointer -= recordSize;
			raf.seek (filePointer);
			ID = raf.readInt ();
		}
		return ID == id;
	}
}

// Local Variables:
// mode: java
// mode: flyspell-prog
// ispell-local-dictionary: "british"
// End:
